import Vue from 'vue'
import App from './App'
import i18n from './i18n'
import store from './store'
import router from './router'
import '@/vab'

import VxeUI from 'vxe-pc-ui'
import 'vxe-pc-ui/lib/style.css'
import VxeUITable from 'vxe-table'
import 'vxe-table/lib/style.css'
import VxeUIPluginExportXLSX from '@vxe-ui/plugin-export-xlsx'
import ExcelJS from 'exceljs'
VxeUI.use(VxeUIPluginExportXLSX, {
  ExcelJS,
})

import VxeUIPluginExportPDF from '@vxe-ui/plugin-export-pdf'
import { jsPDF } from 'jspdf'

VxeUI.use(VxeUIPluginExportPDF, {
  jsPDF,
})

import { baseURL, pwa } from './config'
import { isExternal } from '@/utils/validate'
import { getToken } from '@/utils/token'
import axios from 'axios'
import VxeUIPluginRenderWangEditor, {
  WangEditor,
} from '@vxe-ui/plugin-render-wangeditor'
import '@vxe-ui/plugin-render-wangeditor/dist/style.css'
import * as wangEditor from '@wangeditor/editor'
import '@wangeditor/editor/dist/css/style.css'
// 确保 window.wangEditor 变量存在即表示安装完成
VxeUI.use(VxeUIPluginRenderWangEditor, {
  wangEditor,
  // 自定义上传图片方法
  uploadImageMethod({ file }) {
    const formData = new FormData()
    formData.append('file', file)
    // 设置请求头
    const headers = {
      'Content-Type': 'multipart/form-data',
      Authorization: `Bearer ${getToken()}`,
    }
    return axios
      .post(`${baseURL}/api/upload/image`, formData, {
        headers,
      })
      .then((res) => {
        const {
          data: {
            data: { path },
          },
        } = res
        return {
          url: baseURL + path,
        }
      })
  },
  // 自定义上传视频方法
  uploadVideoMethod({ file }) {
    const formData = new FormData()
    formData.append('file', file)
    return axios.post('/api/pub/upload/single', formData).then((res) => {
      const {
        data: {
          data: { path },
        },
      } = res
      return {
        url: path,
      }
    })
  },
})

// （可选组件）安装后可以独立使用
Vue.use(WangEditor)
Vue.use(VxeUI)
Vue.use(VxeUITable)

/**
 * @description 正式环境默认使用mock，正式项目记得注释后再打包
 */

if (process.env.NODE_ENV === 'production' && !isExternal(baseURL)) {
  const { mockXHR } = require('@/utils/static')
  mockXHR()
}

if (pwa) require('./registerServiceWorker')

Vue.config.productionTip = false
new Vue({
  el: '#app',
  i18n,
  store,
  router,
  render: (h) => h(App),
})
